<?php
include "include/config.inc.php";
$today          = date("m/d/y");
$installmentArr = array();
$loanDrawIdVar  = "";
$selectedParty  = 0;
$toatalAmount   = 0;
$m=0;
$selectParty = "SELECT a.loanDrawId,b.partyId,c.partyName
                  FROM drawinstallment a
	                JOIN loandraw b ON a.loanDrawId = b.loanDrawId
	                JOIN party c ON b.partyId = c.partyId
	               WHERE a.isReceived = 'Y'
	            GROUP BY b.partyId
              ORDER BY loanDrawId";
$selectPartyRes = mysql_query($selectParty);
while($selectPartyRow = mysql_fetch_array($selectPartyRes))
{
  $selectPartyId[$m]   = $selectPartyRow['partyId'];
  $selectPartyName[$m] = $selectPartyRow['partyName'];
  $m++;
}

/////////////////  Date filter variable define start
if(isset($_REQUEST['fromDateYear']))
	$fromDate  = $_REQUEST['fromDateYear']."-".$_REQUEST['fromDateMonth']."-".$_REQUEST['fromDateDay'];
else
	$fromDate  = date("Y-m-d");

if(isset($_REQUEST['toDateYear']))
	$toDate    = $_REQUEST['toDateYear']."-".$_REQUEST['toDateMonth']."-".$_REQUEST['toDateDay'];
else
	$toDate    = date("Y-m-d");
/////////////////  Date filter variable define end

if(isset($_REQUEST['go']))
{
	$selectedParty = $_REQUEST['partyNameSel'];
	$loanDrawIdVar=($_REQUEST['loanDrawId'] == "")?0:$_REQUEST['loanDrawId'];
	if($loanDrawIdVar == 0)
	{
    $i=0;
	  if($_REQUEST['partyNameSel'] == 0)
	  {
 	    $seldrawInstallmentDate = "SELECT a.loanDrawId, a.drawInstallmentId,a.loanDrawId,a.drawInstallmentNo,a.drawInstallmentDate,a.drawInstallmentAmount,a.isReceived,
 	                                      b.partyId,c.partyName
	                                 FROM drawinstallment a
	                                 JOIN loandraw b ON a.loanDrawId = b.loanDrawId
	                                 JOIN party c ON b.partyId = c.partyId
	                                WHERE 1=1
	                                  AND a.drawInstallmentDate >= '".$fromDate."'
	                                  AND a.drawInstallmentDate <= '".$toDate."'
	                                  AND a.isReceived = 'Y'
                               ORDER BY loanDrawId,drawInstallmentId";
	  }
	  else
	  {
	  	$partyId = $_REQUEST['partyNameSel'];
	    $seldrawInstallmentDate = "SELECT a.loanDrawId, a.drawInstallmentId,a.loanDrawId,a.drawInstallmentNo,a.drawInstallmentDate,a.drawInstallmentAmount,a.isReceived,
	                                      b.partyId,c.partyName
	                                 FROM drawinstallment a
	                                 JOIN loandraw b ON a.loanDrawId = b.loanDrawId
	                                 JOIN party c ON b.partyId = c.partyId
	                                WHERE 1=1
	                                  AND a.drawInstallmentDate >= '".$fromDate."'
	                                  AND a.drawInstallmentDate <= '".$toDate."'
	                                  AND a.isReceived = 'Y'
	                                  AND b.partyId = ".$partyId."
                               ORDER BY loanDrawId,drawInstallmentId" ;
	  }
	  $seldrawInstallmentDateRes=mysql_query($seldrawInstallmentDate);
    $currentParyId = 0;
	  while($seldrawInstallmentDateResRow=mysql_fetch_array($seldrawInstallmentDateRes))
	  {
      $installmentArr[$i]['loanDrawId']            = $seldrawInstallmentDateResRow['loanDrawId'];
	    $installmentArr[$i]['drawInstallmentNo']     = $seldrawInstallmentDateResRow['drawInstallmentNo'];
	    $installmentArr[$i]['drawInstallmentId']     = $seldrawInstallmentDateResRow['drawInstallmentId'];
	    $installmentArr[$i]['partyName']             = $seldrawInstallmentDateResRow['partyName'];
	    $installmentArr[$i]['loanDrawId']            = $seldrawInstallmentDateResRow['loanDrawId'];
	    $installmentArr[$i]['drawInstallmentDate']   = date("d-m-Y", strtotime($seldrawInstallmentDateResRow['drawInstallmentDate']));
	    $installmentArr[$i]['installmentAmount']     = $seldrawInstallmentDateResRow['installmentAmount'];
	    $installmentArr[$i]['isReceived']            = $seldrawInstallmentDateResRow['isReceived'];
	    $toatalAmount									               += $seldrawInstallmentDateResRow['installmentAmount'];
	    $i++;
	  }
  }
  else
  {
    $i=0;
    $seldrawInstallmentDate = "SELECT a.drawInstallmentId,a.loanDrawId,a.drawInstallmentNo,a.drawInstallmentDate,a.drawInstallmentAmount,a.isReceived,b.partyId,c.partyName
	                               FROM drawinstallment a
	                               JOIN loandraw b ON a.loanDrawId = b.loanDrawId
	                               JOIN party c ON b.partyId = c.partyId
	                              WHERE 1=1
                                  AND a.drawInstallmentDate >= '".$fromDate."'
                                  AND a.drawInstallmentDate <= '".$toDate."'
                                  AND a.isReceived = 'Y' AND a.loanDrawId='$loanDrawIdVar'
                             ORDER BY loanDrawId,drawInstallmentId";
	  $seldrawInstallmentDateRes=mysql_query($seldrawInstallmentDate);
        $currentParyId = 0;
	  while($seldrawInstallmentDateResRow=mysql_fetch_array($seldrawInstallmentDateRes))
	  {
      $installmentArr[$i]['drawInstallmentNo']     = $seldrawInstallmentDateResRow['drawInstallmentNo'];
	    $installmentArr[$i]['drawInstallmentId']     = $seldrawInstallmentDateResRow['drawInstallmentId'];
	    $installmentArr[$i]['partyName']             = $seldrawInstallmentDateResRow['partyName'];
	    $installmentArr[$i]['loanDrawId']            = $seldrawInstallmentDateResRow['loanDrawId'];
	    $installmentArr[$i]['drawInstallmentDate']   = date("d-m-Y", strtotime($seldrawInstallmentDateResRow['drawInstallmentDate']));
	    $installmentArr[$i]['drawInstallmentAmount'] = $seldrawInstallmentDateResRow['drawInstallmentAmount'];
	    $installmentArr[$i]['isReceived']            = $seldrawInstallmentDateResRow['isReceived'];
	    $toatalAmount									               += $seldrawInstallmentDateResRow['drawInstallmentAmount'];
	    $i++;
	  }
  }
}

if(isset($_POST['sub']))
{
  $arrIA=count($_POST['installmentAmount']);
  $installmentValue=$_POST['InstallmentAmountVal']."<br>";
  $drawInstallmentDate = $_POST['drawInstallmentDate'];
  for($i=0;$i<$arrIA;$i++)
  {
  	if($_POST['installmentAmount'][$i] != "")
  	{
  		$countLoop=$_POST['installmentAmount'][$i]/$installmentValue=$_POST['InstallmentAmountVal'];
  		for($j=0;$j<$countLoop;$j++)
  		{
        $_POST['installmentAmount'][$i];    	
        $_POST['drawInstallmentDate'][$i];    	
        $InstallmentsID = $_POST['InstallmentsID'][$i];
        $InstallmentsIDADD = $InstallmentsID + $j;
        $loanDrawId = $_POST['loanDrawId'][$i];
        $updInstallMent="UPDATE `drawinstallment` SET  `isReceived` =  'Y', `receiveDate` = '".$drawInstallmentDate."'
                          WHERE `drawInstallmentId` =".$InstallmentsIDADD." AND loanDrawId=".$loanDrawId;          
        mysql_query($updInstallMent);
      }
  	}
  }
}

$smarty->assign("today",$today);
$smarty->assign("fromDate",$fromDate);
$smarty->assign("toDate",$toDate);
$smarty->assign("selectPartyId",$selectPartyId);
$smarty->assign("selectPartyName",$selectPartyName);
$smarty->assign("selectedParty",$selectedParty);
$smarty->assign("loanDrawIdVar",$loanDrawIdVar);
$smarty->assign("installmentArr",$installmentArr);
$smarty->assign("toatalAmount",$toatalAmount);
$smarty->display('loandrawreceived.tpl');
?>